import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-08-10
 * Time: 20:27
 */
public class Test {
    public int totalFruit1(int[] fruits) {
        int n = fruits.length;
        int[] map = new int[n];
        int left = 0,right = 0,len = 0,kinds = 0;
        while(right < n) {
            if(map[fruits[right]] == 0) {
                kinds++;
            }
            map[fruits[right]]++;
            while(kinds > 2) {
                map[fruits[left]]--;
                if(map[fruits[left]] == 0) {
                    kinds--;
                }
                left++;
            }
            len = Math.max(len,right - left + 1);
            right++;
        }
        return len;
    }
}
